package my.questionsystem;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class QuestioningSystemActivity extends Activity {
	private EditText userNameInput;
	private EditText passwordInput;
	
	private Http httpRequirements;

	private void networkSetting() {
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
				.penaltyLog().penaltyDeath().build());
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		networkSetting();
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		userNameInput = (EditText) findViewById(R.id.inputUaerName);
		passwordInput = (EditText) findViewById(R.id.inputPassword);
	}

	public void onClick(View view) {

		switch (view.getId()) {
		case R.id.loginBtn:

			if (httpRequirements == null || !httpRequirements.isAlive()) {

				String userName = userNameInput.getText().toString();
				String password = passwordInput.getText().toString();
				String url = "http://10.0.2.2:50530/login/loginName="+userName+"_password="+password+"_type=Client";
		
				httpRequirements = new Http(handler, url);
				httpRequirements.start();
			}
			break;
		}
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			String responseString = (String) msg.obj;
			
			if (responseString != null && !responseString.equals( "\"Failed\"") ) {
				
				Intent fornextpage = new Intent();
				
				fornextpage.setClass(QuestioningSystemActivity.this,
						SelectSet.class);
			
				startActivity(fornextpage);
				
			} else {
				Toast.makeText(QuestioningSystemActivity.this, "Invalid Login",Toast.LENGTH_LONG).show();
			}
		}
	};
	

	}